package com.songyang.blog.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.songyang.blog.entity.Category;
import com.songyang.blog.service.CategoryService;
import com.songyang.blog.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

@Configuration
public class ContextStartUp implements ApplicationRunner, ServletContextAware {
    //项目启动的时候就会运行这个方法
    @Autowired
    CategoryService categoryService;
    @Autowired
    PostService postService;
    @Autowired
    ServletContext servletContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Category> categories = categoryService.list(new QueryWrapper<Category>().eq("status", 0));

        postService.initWeekRank();
        servletContext.setAttribute("categorys", categories);

    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
